package de.ventu.controls
{	
	import de.ventu.containers.ImageFlowBox;
	
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	
	import mx.containers.Box;
	import mx.controls.Image;
	import mx.core.Application;
	
	import uk.soulwire.utils.display.Alignment;
	import uk.soulwire.utils.display.DisplayUtils;
	
	
	public class ImageObject extends Box
	{
		public static const THUMB_HEIGHT:Number = 120;

		private var _application:Object;
		private var _selectable:Boolean;
		
		public var thumbWidth:Number;
		public var thumbHeight:Number;
		
		public var image:Image;
		public var imageUrl:String;
		public var imageFlowBox:ImageFlowBox;
		
		public var selected:Boolean;
		
		public function ImageObject(url:String, imageFlowBoxObject:Object)
		{
			super();
			
			// initialize member variables
			_application = Application.application;
			
			thumbWidth = 0;
			thumbHeight = THUMB_HEIGHT;
			
			image = new Image();
			imageUrl = url;
			imageFlowBox = ImageFlowBox(imageFlowBoxObject);
			
			selected = false;
			_selectable = true;
						
			// set style
			setStyle("horizontalAlign", "center");
			setStyle("verticalAlign", "middle");
			
			// enable double click (default is off)
			image.doubleClickEnabled = true;

			// add eventlistener
			image.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			image.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			image.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			image.addEventListener(MouseEvent.DOUBLE_CLICK, thumbToFull);
			image.addEventListener(MouseEvent.RIGHT_CLICK, mouseRightClickHandler);
			
			// multitouch eventlistener and handler
			image.addEventListener(TouchEvent.MOUSE_DOWN, downHandler);
			image.addEventListener(TouchEvent.MOUSE_OVER, overHandler);
			image.addEventListener(TouchEvent.MOUSE_OUT, outHandler);
			image.addEventListener(TouchEvent.DOUBLE_CLICK, thumbToFull);

			// load image
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
			loader.load(new URLRequest(imageUrl));
		}
		
		private function loadCompleteHandler(event:Event):void
		{
			var bitmap:Bitmap = Bitmap(Loader(event.target.loader).content);
		    
		    thumbWidth = (bitmap.width / bitmap.height) * thumbHeight;

		    image.width = thumbWidth;
		    image.height = thumbHeight;
            image.source = DisplayUtils.createThumb(bitmap.bitmapData, thumbWidth, thumbHeight, Alignment.MIDDLE, true);
            image.filters = [getDropShadowFilter()];
			
			addChild(image);
			
			bitmap = null;
			
			height = thumbHeight;
			width = thumbWidth;
			
			imageFlowBox.addImageObject(this);
		}
		
		private function getDropShadowFilter():DropShadowFilter
		{
			var color:Number = 0x000000;
            var angle:Number = 45;
            var alpha:Number = 0.6;
            var blurX:Number = 4;
            var blurY:Number = 5;
            var distance:Number = 1;
            var strength:Number = 1;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.HIGH;
            
            return new DropShadowFilter(distance, angle, color, alpha, blurX, blurY, strength, quality, inner, knockout);
		}
		
		private function downHandler(event:Event):void
		{
			_selectable = false;
			selectOrDeselect();
		}
		
		private function overHandler(event:Object):void
		{
			if(event.buttonDown && _selectable)
			{
				_selectable = false;
				selectOrDeselect();
			}
		}
		
		private function outHandler(event:Event):void
		{
			_selectable = true;
		}
		
		private function selectOrDeselect():void
		{
			if(selected)
				deselectImage();
			else
				selectImage();
		}
		
		public function selectImage():void
		{
			imageFlowBox.lastSelectedImageUrl = imageUrl;
			
			selected = true;
			
			graphics.beginFill(0xFFFFFF, 1);
			graphics.drawRect(-7, -7, width + 14, height + 14);
			image.alpha = 0.5;
			image.blendMode = BlendMode.DARKEN;
		}
		
		public function deselectImage():void
		{
			selected = false;
			
			graphics.clear();
			image.alpha = 1;
			image.blendMode = BlendMode.NORMAL;
		}
		
		public function thumbToFull(event:Event):void
		{
			imageFlowBox.showImageFullscreen(this);
		}
				
		private function mouseRightClickHandler(event:MouseEvent): void
		{
			imageFlowBox.showImageSplitScreen(this);
		}			
	}
}